package com.dongdongshop.controller;

import ch.qos.logback.core.encoder.EchoEncoder;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.data.ResultEnum;
import com.dongdongshop.pojo.address.TbAddress;
import com.dongdongshop.pojo.cities.TbAreas;
import com.dongdongshop.pojo.cities.TbCities;
import com.dongdongshop.pojo.cities.TbProvinces;
import com.dongdongshop.pojo.user.TbUser;
import com.dongdongshop.service.address.AddressService;
import com.dongdongshop.service.address.AreasServiceI;
import com.dongdongshop.service.address.CitieService;
import com.dongdongshop.service.address.ProvincesServiceI;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("address")
public class AddressController {
    private final Logger logger= LoggerFactory.getLogger(AddressController.class);

    @Reference
    private AddressService addressService;
    @Reference
    private ProvincesServiceI provincesServiceI;
    @Reference
    private CitieService citieService;
    @Reference
    private AreasServiceI areasServiceI;
    @RequestMapping("index")
    public String index(Model model){
        return "home-index";
    }

    @RequestMapping("toList")
    public String  toList(Model model){
        TbUser user =(TbUser) SecurityUtils.getSubject().getPrincipal();
        if (user == null){
            return "redirect:user/toLogin";
        }
        model.addAttribute("user",user);
        return "home-setting-address";
    }

    //查询
    @RequestMapping("getAddress")
    @ResponseBody
    public Result getAddress(){
        System.out.println("---------------");
        try {
            //获取用户登录信息
            TbUser user =(TbUser) SecurityUtils.getSubject().getPrincipal();
            List<TbAddress> listAddress=addressService.getAddress(user.getUsername());
            for (TbAddress address : listAddress) {
               TbProvinces provinces= provincesServiceI.getById(address.getProvinceId());
               address.setProvinceName(provinces.getProvince());
               TbCities cities=citieService.getById(address.getCityId());
               address.setCityName(cities.getCity());
                TbAreas areas=areasServiceI.getById(address.getTownId());
                address.setAreaName(areas.getArea());
            }
            System.out.println("list++++++"+listAddress);
            return Result.response(ResultEnum.OK).setData(listAddress);
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.FAIL);
        }
    }

    //删除
    @RequestMapping("deleteById/{id}")
    @ResponseBody
    public Result deleteById(@PathVariable("id")Long id){
        try {
            int i=addressService.deleteById(id);
            if (i>0){
               return Result.response(ResultEnum.OK);
            }else {
                return Result.response(ResultEnum.FAIL);
            }
        }catch (Exception e){
            return Result.response(ResultEnum.FAIL);
        }
    }
    //修改默认设置
    @RequestMapping("setDefault/{id}")
    @ResponseBody
    public Result setDefault(@PathVariable("id")Long id){
        try {
            addressService.setDefault(id);
            return Result.response(ResultEnum.OK);
        }catch (Exception e){
            return Result.response(ResultEnum.FAIL);
        }
    }
    //省市县
    @RequestMapping("getProvinces")
    @ResponseBody
    public Result getProvinces(){
        try {
            List<TbProvinces> provincesList=provincesServiceI.getProvinces();
            return Result.response(ResultEnum.OK).setData(provincesList);
        }catch (Exception e){
            return Result.response(ResultEnum.FAIL);
        }
    }
    @RequestMapping("getCity/{cityId}")
    @ResponseBody
    public Result getCity(@PathVariable("cityId")String cityId){
        try {
            List<TbCities> citiesList=citieService.getCity(cityId);
            return Result.response(ResultEnum.OK).setData(citiesList);
        }catch (Exception e){
            return Result.response(ResultEnum.FAIL);
        }
    }
    @RequestMapping("getArea/{areaId}")
    @ResponseBody
    public Result getArea(@PathVariable("areaId")String areaId){
        try {
            List<TbAreas> areasList=areasServiceI.getArea(areaId);
            return Result.response(ResultEnum.OK).setData(areasList);
        }catch (Exception e){
            return Result.response(ResultEnum.FAIL);
        }
    }

    //新增
    @RequestMapping("addAddress")
    @ResponseBody
    public Result addAddress(TbAddress tbAddress){
        try {
            //获取登录用户
            TbUser user =(TbUser) SecurityUtils.getSubject().getPrincipal();
            //增加用户名称
            tbAddress.setUserId(user.getUsername());
            tbAddress.setCreateDate(new Date());
            addressService.addAddress(tbAddress);
            return Result.response(ResultEnum.OK);
        }catch (Exception e){
            return Result.response(ResultEnum.FAIL);
        }
    }
}
